package com.kaili.basic.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.MdmMessageAddDto;
import com.kaili.basic.pojo.dto.MdmMessageQueryDto;
import com.kaili.basic.pojo.dto.MdmMessageUpdateDto;
import com.kaili.basic.pojo.entity.MdmMessage;
import com.kaili.basic.pojo.entity.MdmMessageExport;
import com.kaili.basic.pojo.vo.MdmMessageVO;
import com.kaili.basic.service.IMessageService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.aop.OperateLogAspect;
import com.kaili.common.aop.log.enums.FunctionName;
import com.kaili.common.aop.log.enums.OperateType;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.CommonUtil;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Desc: mdm消息管理 前端控制器
 * @author: dengmengna
 * @Time: ${DATE} ${TIME}
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
 */
@RestController
@ApiVersion(1)
@RequestMapping("{api_version}")
@Api(tags = "消息管理", description = "mdm消息管理相关API")
public class MdmMessageController {

    /**
    *  mdm消息service对象
    */
    @Autowired
    IMessageService mdmMessageService;

    @Autowired
    private MessageSource messageSource;


    /**
    * @Desc:  mdm消息新增
    * @author: dengmengna
    * @Time: 2019/7/26 9:05
    * @param mdmMessageAddDto
    * @return com.kaili.common.response.RestStatus
    */
    @PostMapping(value = "/messages/addMdmMessage")
    @ApiOperation(value = "新增mdm消息",notes = "新增mdm消息,messageTitle、noticeId、content、validity、plantformId是必填的，其他不必填，必看必回设置：1为不必看，2为必看，3为必回")
    @OperateLogAspect(functionName = FunctionName.mdmMessage,operateType = OperateType.ADD, key = "messageTitle")
    public RestStatus addMdmMessage(@RequestBody MdmMessageAddDto mdmMessageAddDto){
        MdmMessage mdmMessage = new MdmMessage();
        BeanUtils.copyProperties(mdmMessageAddDto, mdmMessage);
        mdmMessage.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        mdmMessageService.addMdmMessage(mdmMessage);
        return CommonStatus.ADD_OK;
    }

    /**
    * @Desc:  mdm单个消息修改
    * @author: dengmengna
    * @Time: 2019/7/26 10:03
    * @param mdmMessageUpdateDto
    * @return com.kaili.common.response.RestStatus
    */
    @PutMapping(value = "/messages/updateMdmMessage")
    @ApiOperation(value = "修改mdm消息",notes = "修改单个mdm消息，必须要传消息的id，其他都是可选")
    @OperateLogAspect(functionName = FunctionName.mdmMessage,operateType = OperateType.UPDATE, key = "messageTitle")
    public RestStatus updateMdmMessage(@RequestBody MdmMessageUpdateDto mdmMessageUpdateDto){
        if(mdmMessageUpdateDto!=null && mdmMessageUpdateDto.getSendRanges()!=null && mdmMessageUpdateDto.getSendRange() == null){
            String str = "";
            for (Integer i:mdmMessageUpdateDto.getSendRanges()
                 ) {
                str += i +",";
            }
            mdmMessageUpdateDto.setSendRange(str.substring(0,str.length()-1));
        }
        mdmMessageUpdateDto.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        mdmMessageService.updateMdmMessage(mdmMessageUpdateDto);
        return CommonStatus.UPDATE_OK;
    }

    /**
    * @Desc:  mdm消息批量修改
    * @author: dengmengna
    * @Time: 2019/7/26 10:03
    * @param mdmMessageUpdateDto
    * @param ids
    * @return com.kaili.common.response.RestStatus
    */
    @PutMapping(value = "/messages/{ids}/updateMdmMessages")
    @ApiOperation(value = "批量修改mdm消息",notes = "批量修改mdm消息，至少要传一个消息的id，其他都是可选，消息的id凭借到地址上，例如/messages/1,2,3/updateMdmMessages")
    @OperateLogAspect(functionName = FunctionName.mdmMessage,operateType = OperateType.UPDATE, key = "messageTitle")
    public RestStatus updateMdmMessages(@RequestBody MdmMessageUpdateDto mdmMessageUpdateDto, @PathVariable Long[] ids){

        /*MdmMessage mdmMessage = new MdmMessage();
        BeanUtils.copyProperties(mdmMessageUpdateDto, mdmMessage);*/
        mdmMessageUpdateDto.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        if(mdmMessageUpdateDto!=null && mdmMessageUpdateDto.getSendRanges()!=null && mdmMessageUpdateDto.getSendRange() == null){
            String str = "";
            for (Integer i:mdmMessageUpdateDto.getSendRanges()
            ) {
                str += i +",";
            }
            mdmMessageUpdateDto.setSendRange(str.substring(0,str.length()-1));
        }
        if(mdmMessageUpdateDto.getSendRanges()!=null){
            mdmMessageUpdateDto.setSendRange(StringUtils.join(mdmMessageUpdateDto.getSendRanges(), ","));
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("mdmMessageUpdateDto",mdmMessageUpdateDto);
        map.put("ids",ids);
        System.out.println("#####"+map);
        mdmMessageService.updateMdmMessages(map);
        return CommonStatus.UPDATE_OK;
    }

    /**
    * @Desc:  mdm消息批量删除
    * @author: dengmengna
    * @Time: 2019/7/26 10:04
    * @param ids
    * @return com.kaili.common.response.RestStatus
    */
    @DeleteMapping("/messages/{ids}/deleteMdmMessages")
    @ApiOperation(value = "删除mdm消息", notes = "删除mdm消息,至少要传一个消息的id")
    @OperateLogAspect(functionName = FunctionName.mdmMessage,operateType = OperateType.DELETE)
    public RestStatus deleteMessages(@PathVariable Long[] ids) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("modifyBy", LoginDetailUtils.getCurrentEmployeeId().longValue());
        map.put("ids",ids);
        mdmMessageService.deleteMdmMessages(map);
        return CommonStatus.DELETE_OK;
    }

    /**
    * @Desc:  条件查询获取mdm消息信息
    * @author: dengmengna
    * @Time: 2019/7/26 10:04
    * @param mdmMessageQueryDto
    * @return com.baomidou.mybatisplus.plugins.Page<com.kaili.basic.pojo.vo.MdmMessageVO>
    */
    @GetMapping(value = "mdmMessages/queryMdmMessages")
    @ApiOperation(value = "条件查询mdm消息",notes = "条件查询mdm消息,条件都是不必填，过期条件中：未过期_1,已过期_2,发布状态条件中：未发布_1,已发布_2,必看必回设置：1为不必看，2为必看，3为必回")
    public Page<MdmMessageVO> queryMdmMessages(@ModelAttribute MdmMessageQueryDto mdmMessageQueryDto) {
        return mdmMessageService.queryMdmMessage(new Page<MdmMessageVO>(mdmMessageQueryDto.getPage(),mdmMessageQueryDto.getPageSize()),mdmMessageQueryDto);

    }

    /**
    * @Desc:  通过id查询mdm消息
    * @author: dengmengna
    * @Time: 2019/7/31 10:18
    * @param id
    * @return com.kaili.basic.pojo.vo.MdmMessageVO
    */
    @GetMapping(value = "mdmMessages/queryMdmMessageById/{id}")
    @ApiOperation(value = "通过id查询mdm消息",notes = "参数id")
    public MdmMessageVO queryMdmMessageById(@PathVariable Long id) {
        return mdmMessageService.queryMdmMessageById(id);
    }


    /**
    * @Desc:  mdm消息导出
    * @author: dengmengna
    * @Time: 2019/7/26 10:04
    * @param response
    * @param mdmMessageQueryDto
    * @return void
    */
    @GetMapping("mdmMessages/export")
    @ApiOperation(value = "导出消息，查询条件可选")
    public void export(HttpServletResponse response, @ModelAttribute MdmMessageQueryDto mdmMessageQueryDto) throws RestException {
        String msg = CommonUtil.getMessage(messageSource,"mdmMessage");
        mdmMessageQueryDto.setPageSize(Integer.MAX_VALUE);
        mdmMessageQueryDto.setPage(1);
        List<MdmMessageExport> list = mdmMessageService.queryMdmMessageExport(mdmMessageQueryDto);
        ExcelUtil.exportExcel(list,"MDM消息","sheet1", MdmMessageExport.class, UUID.randomUUID().toString()+".xls",response);
    }
    @GetMapping(value = "mdmMessages/queryMdmMessageBySN/{SN}")
    @ApiOperation(value = "通过SN查询mdm消息，有效期大于当前时间",notes = "参数SN")
    public List<MdmMessageVO> queryMessageBySN(@PathVariable Long SN){
        return mdmMessageService.queryMessageBySN(SN);
    }
}

